package com.tadpole.music.presenter.me;

import com.tadpole.music.bean.me.VipListBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.me.VipListIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员中心
 * Created By 孙岩
 * on 2020/9/22
 */
public class VipListPresenter extends BasePresenter<VipListIView> {
    private String url;

    /**
     * @param flag       1是其他（带参数college_id，rank_id） 2是我的（不带参数）
     * @param college_id 学院id
     * @param rank_id    等级id
     */
    @SuppressWarnings("unchecked")
    public void getVipList(String flag, String college_id, String rank_id, final boolean isRefresh) {
        if (isViewAttached()) {
            if ("1".equals(flag)) {
                url = UrlConfig.getVipList + "?college_id=" + college_id + "&rank_id=" + rank_id;
            } else if ("2".equals(flag)) {
                url = UrlConfig.getVipList;
            }

            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    List<VipListBean.DataBean> list = new ArrayList<>();
                                    JSONArray array = new JSONArray(obj.getString("data"));
                                    for (int i = 0; i < array.length(); i++) {
                                        JSONObject object = new JSONObject(array.get(i).toString());

                                        JSONObject jsonObject = new JSONObject(object.getString("rank"));
                                        VipListBean.DataBean.RankBean rankBean = new VipListBean.DataBean.RankBean(
                                                JsonParseUtil.getInt(jsonObject, "id"),
                                                JsonParseUtil.getStr(jsonObject, "name")
                                        );

                                        JSONObject collegeObj = new JSONObject(object.getString("college"));
                                        VipListBean.DataBean.CollegeBean collegeBean = new VipListBean.DataBean.CollegeBean(
                                                JsonParseUtil.getStr(collegeObj, "name")
                                        );

                                        VipListBean.DataBean bean = new VipListBean.DataBean(
                                                JsonParseUtil.getInt(object, "id"),
                                                JsonParseUtil.getStr(object, "name"),
                                                JsonParseUtil.getStr(object, "thumb"),
                                                JsonParseUtil.getStr(object, "app_thumb"),
                                                JsonParseUtil.getStr(object, "desc"),
                                                JsonParseUtil.getInt(object, "college_id"),
                                                collegeBean,
                                                JsonParseUtil.getDouble(object, "price"),
                                                JsonParseUtil.getBoolean(object, "paid"),
                                                rankBean,
                                                JsonParseUtil.getStr(object, "expire_at")
                                        );
                                        list.add(bean);
                                    }

                                    if(isRefresh){
                                        getView().showVipList(list);
                                    }else{
                                        getView().showVipList(list);
                                    }


                                } else if (obj.getInt("code") == 401) {
                                    getView().show401();
                                } else {
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                            getView().hideLoading();
                            getView().stopRefresh();
                        }

                        @Override
                        public void onComplete() {
                            getView().hideLoading();
                            getView().stopRefresh();
                        }

                        @Override
                        public void onBefore() {
                            getView().showLoading();
                        }
                    });
        }
    }
}
